package com.wisezone.workflow.core.impl;

import java.util.Date;
import java.util.Map;

import com.wisezone.common.Pagination;
import com.wisezone.common.exception.WzServiceException;
import com.wisezone.workflow.bean.WorkFlowEntity;
import com.wisezone.workflow.bean.form.WorkFlowForm;
import com.wisezone.workflow.bean.search.WorkFlowSearch;
import com.wisezone.workflow.core.FlowEngine;
import com.wisezone.workflow.core.ProcessInstance;
import com.wisezone.workflow.core.WorkFlowService;

/**
 * 工作流辅助类 2010-08-18
 * 
 * @author shan
 * 
 */
public class WorkFlowServiceImpl implements WorkFlowService {

	private FlowEngine flowEngine;

	public<T> Pagination<T> listFormByPage(WorkFlowSearch searchEntity)
			throws WzServiceException {

		return flowEngine.listProcessInstByPage(searchEntity);
	}

	public WorkFlowEntity start(WorkFlowEntity entity, String key, Long userId,
			Long[] nextUserIds, Long[] nextUserGroupIds, String opinion,
			Map<String, Object> variableMap,
			Map<String, Object> localVariableMap) throws WzServiceException {

		WorkFlowSearch searchEntity = new WorkFlowSearch();
		searchEntity.setKey(key);
		searchEntity.setActorId(userId);
		searchEntity.setOpinion(opinion);
		if (nextUserIds != null && nextUserIds.length > 0) {
			searchEntity.setUserIds(nextUserIds);
		}
		if (nextUserGroupIds != null && nextUserGroupIds.length > 0) {
			searchEntity.setUserGroupIds(nextUserGroupIds);
		}
		if (variableMap != null && !variableMap.isEmpty()) {
			searchEntity.setVariableMap(variableMap);
		}
		if (localVariableMap != null && !localVariableMap.isEmpty()) {
			searchEntity.setLocalVariableMap(localVariableMap);
		}
		WorkFlowForm form = flowEngine.complateStartActivity(searchEntity);

		entity.setProcessInstId(form.getProcessInstId());
		entity.setStartTime(new Date());
		entity.setStatusId(Long.valueOf(form.getStatusId()));
		entity.setStartUserId(userId);
		return entity;
	}

	public WorkFlowEntity operate(WorkFlowEntity entity, Long activityInstId,
			Long userId, Long linkId, Long[] nextUserIds,
			Long[] nextUserGroupIds, String opinion,
			Map<String, Object> variableMap,
			Map<String, Object> localVariableMap) throws WzServiceException {

		WorkFlowSearch searchEntity = new WorkFlowSearch();
		searchEntity.setActivityInstId(activityInstId);
		searchEntity.setActorId(userId);
		if (linkId != null) {
			searchEntity.setLinkId(linkId);
		}
		searchEntity.setOpinion(opinion);
		if (nextUserIds != null && nextUserIds.length > 0) {
			searchEntity.setUserIds(nextUserIds);
		}
		if (nextUserGroupIds != null && nextUserGroupIds.length > 0) {
			searchEntity.setUserGroupIds(nextUserGroupIds);
		}
		if (variableMap != null && !variableMap.isEmpty()) {
			searchEntity.setVariableMap(variableMap);
		}
		if (localVariableMap != null && !localVariableMap.isEmpty()) {
			searchEntity.setLocalVariableMap(localVariableMap);
		}
		WorkFlowForm form = flowEngine.operateActivity(searchEntity);

		entity.setStatusId(Long.valueOf(form.getStatusId()));
		return entity;
	}

	public void delete(Long processInstId, Long userId)
			throws WzServiceException {

		ProcessInstance processInst = this.flowEngine
				.loadProcessInstance(processInstId);
		processInst.logicDelActivityInstList(userId);
	}

	public void setFlowEngine(FlowEngine flowEngine) {
		this.flowEngine = flowEngine;
	}

}
